package com.kid.keep.controller.impl;

import com.honeybee.cloud.framework.common.response.ResponseEntity;
import com.kid.keep.common.SessionStore;
import com.kid.keep.common.UserSubject;
import com.kid.keep.controller.IMessageController;
import com.kid.keep.dto.MessageDTO;
import com.kid.keep.enums.MessageTypeEnum;
import com.kid.keep.mapper.ICheckItemCovertMapper;
import com.kid.keep.mapper.IMessageCovertMapper;
import com.kid.keep.request.AddUserRequest;
import com.kid.keep.service.IMessageService;
import com.kid.keep.service.impl.MessageService;
import com.kid.keep.vo.AddUserVO;
import com.kid.keep.vo.MessageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description 消息
 * @Author JOHN
 * @Create 2020-01-13
 * @Since 1.0.0
 */
@RestController
public class MessageController implements IMessageController {
    private IMessageService messageService;
    private IMessageCovertMapper messageCovertMapper;

    @Autowired
    public void setMessageCovertMapper(IMessageCovertMapper messageCovertMapper) {
        this.messageCovertMapper = messageCovertMapper;
    }

    @Autowired
    public MessageController(IMessageService messageService) {
        this.messageService = messageService;
    }

    @Override
    public ResponseEntity<Integer> getUnReadMessage() {
        UserSubject userSubject = SessionStore.getSessionUser();
        Integer number = messageService.countNewMessages(userSubject.getUserId());
        return ResponseEntity.ok(number);
    }

    @Override
    public ResponseEntity<List<MessageVO>> getUnReadMessageList() {
        UserSubject userSubject = SessionStore.getSessionUser();
        List<MessageDTO> messageDTOS = messageService.listNewMessagesByType(MessageTypeEnum.PERSON.getValue(), userSubject.getUserId());
        return ResponseEntity.ok(messageCovertMapper.toVO(messageDTOS));
    }

    @Override
    public ResponseEntity<Void> readMessage(Long messageId) {
        UserSubject userSubject = SessionStore.getSessionUser();
        messageService.readMessage(messageId, userSubject.getUserId());
        return ResponseEntity.ok();
    }

    @Override
    public ResponseEntity<Void> readMessage() {
        UserSubject userSubject = SessionStore.getSessionUser();
        messageService.readMessage(userSubject.getUserId());
        return ResponseEntity.ok();
    }
}
